package com.sample.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.bdt.SampleBdt;
import com.sample.service.SampleService;

@Controller
//@RequestMapping("/simple")
public class SampleController {

	private static final Logger log = LoggerFactory.getLogger(SampleController.class);

	@Autowired
	private SampleService service;

	// RESTful method
	@RequestMapping(value="/sample", produces={MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE})
	@ResponseStatus(HttpStatus.OK)
	@ResponseBody
	public List<SampleBdt> getListMediaType(Model model) {
		log.debug("sample getListMediaType Start");
		return service.getList();
	}

	// View-based method
	@RequestMapping(value="/sample")
	public String getList(Model model) {
		log.debug("sample getList Start");
		List<SampleBdt> list = service.getList();
		log.debug("sample getList {}", list);
		model.addAttribute("list", list);
		log.debug("sample getList End");
		return "sample/list";
	}

	@RequestMapping("/sampleTran")
	@ResponseBody
	public List<SampleBdt> getTranList(@ModelAttribute("SampleBdt") SampleBdt bdt, Model model){
		log.debug("getTranList", bdt);
		return service.setTranList(bdt);
	}

	@RequestMapping("/sample/{num}")
	@ResponseBody
	public SampleBdt getSample(@PathVariable("num") int num) {
		return service.getSample(num);
	}

	@RequestMapping(value="/sample/insert", method=RequestMethod.POST)
	public int insertSample(@ModelAttribute("SampleBdt") SampleBdt bdt) {
		return service.insertSample(bdt);
	}

	@RequestMapping(value="/sample/update", method=RequestMethod.POST)
	public int updateSample(@ModelAttribute("SampleBdt") SampleBdt bdt) {
		return service.updateSample(bdt);
	}

	@RequestMapping(value="/sample/delete", method=RequestMethod.POST)
	public int deleteSample(@RequestParam("num") int num) {
		return service.deleteSample(num);
	}
}
